const { v4: uuidv4 } = require("uuid");

/**
 * 菜单表模型
 */
module.exports = (app) => {
  const { STRING, BIGINT, BOOLEAN, DATE, DOUBLE, UUID ,INTEGER} = app.Sequelize;
  const Permission = app.model.define(
    "permission",
    {
      id: {
        type: UUID,
        unique: true,
        primaryKey: true,
        allowNull: false,
        defaultValue: () => {
          return uuidv4().replace(/-/g, "");
        },
      },
      code: {
        type: STRING,
        allowNull: true,
      },
      name: {
        type: STRING,
        allowNull: true,
      },
      title: {
        type: STRING,
        allowNull: true,
      },
      path: {
        type: STRING,
        allowNull: true,
      },
      component: {
        type: STRING,
        allowNull: true,
      },
      sort:{
        type: BIGINT,
        allowNull: true,
      },
      created: {
        type: DATE,
        allowNull: true,
        defaultValue: app.Sequelize.NOW,
      },
      creator: {
        type: STRING(36),
        allowNull: true,
      },
      edited: {
        type: DATE,
        allowNull: true,
      },
      editor: {
        type: STRING(36),
        allowNull: true,
      },
      delFlag: {
        type: INTEGER,
        allowNull: false,
        defaultValue: 0,
      },
    },
    {
      freezeTableName: true, // Model 对应的表名将与model名相同
      timestamps: false,
    }
  );

  Permission.associate = function () {
    app.model.Permission.belongsToMany(app.model.Role, {
      through: app.model.RolePermission,
      foreignKey: "permissionId",
      otherKey: "roleId",
    });
  };

  return Permission;
};
